package com.jakewharton.rxbinding4.component;

import static com.jakewharton.rxbinding4.internal.Preconditions.checkMainThread;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.openharmony.MainThreadDisposable;
import ohos.agp.components.RadioContainer;

final class RadioContainerMarkChangedObservable extends Observable<RadioContainerCheckedChangedEvent> {
    private final RadioContainer view;

    RadioContainerMarkChangedObservable(RadioContainer view) {
        this.view = view;
    }

    @Override
    protected void subscribeActual(Observer<? super RadioContainerCheckedChangedEvent> observer) {
        if (!checkMainThread(observer)) {
            return;
        }
        Listener listener = new Listener(view, observer);
        observer.onSubscribe(listener);
        view.setMarkChangedListener(listener);
    }

    static final class Listener extends MainThreadDisposable implements RadioContainer.CheckedStateChangedListener {
        private final RadioContainer view;
        private final Observer<? super RadioContainerCheckedChangedEvent> observer;

        Listener(RadioContainer view, Observer<? super RadioContainerCheckedChangedEvent> observer) {
            this.view = view;
            this.observer = observer;
        }

        @Override
        protected void onDispose() {
            view.setMarkChangedListener(null);
        }

        @Override
        public void onCheckedChanged(RadioContainer radioContainer, int i) {
            if (!isDisposed()) {
                RadioContainerCheckedChangedEvent event = RadioContainerCheckedChangedEvent.create(radioContainer, i);
                observer.onNext(event);
            }
        }
    }
}
